<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use EasyWeChat\Factory;

/**
 * 示例接口
 */
class Demo extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
  
    protected $noNeedRight = ['*'];
  
    public $config = [
          'corp_id' => 'wxa8333bbf9962d0e0',//企业ID
          'secret'   => 'rnvyJF7szB2yCU6zttRdjSlq5M3AYcfQtYYadPGdFKg',
      ];
    
  
  	public function jssdk()
    {
        header ('Access-Control-Allow-Origin:*');
      	$url = $this->request->request('url');
        $app = Factory::work($this->config);
        $app->jssdk->setUrl($url);
        $json = $app->jssdk->buildConfig(array('checkJsApi','selectEnterpriseContact','openEnterpriseChat','getLocation','openLocation'));
      	$data = json_decode($json,true);
      	$data['beta'] = true;
        $data['beta'] = true;

        $this->success('返回成功', $data);
    }


    /**
     * 无需登录的接口
     * 
     */
    public function test1()
    {
        $code = "U0PsP2Iq5tmikd8yfZdnHG7CKFM1p_1Ciz27JlUtfXI";//$this->request->request('code');
        $config = $this->getConfig($this->getDefault('Trajectory'));
        $app = Factory::work($config);
        $accessToken = $app->access_token;
        $token = $accessToken->getToken();
        $data = \fast\Http::get("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$token['access_token']}&code={$code}");
        $this->success('返回成功', ['action' => 'test1']);
    }

    /**
     * 需要登录的接口
     * 
     */
    public function test2()
    {
        list($stat,$pass,$notAudit,$not) = \app\common\model\task\Outer::getOuter(1);

        $this->success('返回成功', ['stat' => $stat,'pass'=>$pass,'notAudit'=>$notAudit,'not'=>$not]);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     * 
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }

    public function postGet()
    {
        print_r($this->request->request());
    }
  
    /**
     * 读取省市区数据,联动列表
     */
    public function area()
    {
        $where['level'] = 1;
        $provincelist = Db::name('area')->where($where)->field('id,name as value')->select();
//        $provincelist = array_column($provincelist,null ,'id');
        foreach ($provincelist as &$province)
        {
            $where['level'] = 2;
            $where['pid'] = $province['id'];
            $province['childs'] = Db::name('area')->where($where)->field('id,name as value')->select();
//            $province['childs'] = array_column($province['childs'],null ,'id');
            foreach ($province['childs'] as &$area)
            {
                $where['level'] = 3;
                $where['pid'] = $area['id'];
                $area['childs'] = Db::name('area')->where($where)->field('id,name as value')->select();
//                $area['childs'] = array_column( $area['childs'],null ,'id');
            }
        }
        $this->success('', $provincelist);
    }
}
